package com.scalar.chiptrack.adaptor.oset;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.TestLot;
import com.scalar.chiptrack.adaptor.InventoryLot;
import com.scalar.chiptrack.adaptor.osep.OSEPWipStatus;
import com.scalar.chiptrack.adaptor.dao.TestAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.InventoryLotDAO;
import com.scalar.chiptrack.adaptor.dao.AssemblyAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.adaptor.dao.AdaptorDAO;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.ScalarException;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Calendar;
import java.io.File;

import jxl.Workbook;
import jxl.Sheet;
import jxl.Cell;
import jxl.CellType;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Jul 16, 2007
 * Time: 4:43:10 PM
 */
public class OSETDataParser extends JobHandler implements AdaptorConstants
{

    private static Logger m_parserLogger    = null;
    private String m_dataOutputDir          = null;
    HashMap m_assemblyDataMap               = new HashMap();
    HashMap m_ftDataMap                   = new HashMap();
    HashMap m_fgDataMap                     = new HashMap();
    HashMap m_trDataMap                     = new HashMap();
    ArrayList osetAssemblyList              = new ArrayList();

     Calendar cal = Calendar.getInstance();
    int thisYear = cal.get( Calendar.YEAR );
    int thisMonth = cal.get( Calendar.MONTH ) + 1 ;

    public OSETDataParser() throws ScalarException
    {
        try
        {
            m_dataOutputDir = AdaptorUtils.getDataOutputDir(VENDOR_NAME_OSET, WIP_TYPE_ASSEMBLY);
            AdaptorUtils.makeAllAdaptorDirs(m_dataOutputDir);
            m_parserLogger = AdaptorUtils.getDataParserLogger(m_dataOutputDir,VENDOR_NAME_OSET);
            AssemblyAdaptorDAO.setLogger(m_parserLogger);
            InventoryLotDAO.setLogger(m_parserLogger);
            TestAdaptorDAO.setLogger(m_parserLogger);
            AdaptorUtils.setLogger(m_parserLogger);
        }
        catch (ScalarException se)
        {
            log("ERROR!! ERROR!! ERROR!! Exception while loading OSET WIP output directory details from database, Exception: " + se);
            throw se;
        }
    }

    public static void main(String args[]) throws Exception
    {
       new OSETDataParser().execute();
    }


    public void execute() throws Exception
    {
        AdaptorUtils.printHeader(VENDOR_NAME_OSET, CONSTANT_PARSER);

        File doneDestDir = new File(m_dataOutputDir + "/done");

        File wipDataoutDir = new File(m_dataOutputDir);

        File[] fileList = wipDataoutDir.listFiles();

        if ((fileList == null) || (fileList.length <= 3))
        {
            log("No file found in directory: " + wipDataoutDir);
            System.out.println("No file found in directory: " + wipDataoutDir);
            throw new ScalarException("No file found in directory: " + wipDataoutDir + "----\n\n");
        }

        ArrayList allWipLotList = null;
        ArrayList successFileList = new ArrayList();
        boolean success = false;

        //PARSING STARTS FROM HERE
        for (int i = 0; i < fileList.length; i++)
        {
            if (fileList[i].isDirectory() == true)
            {
                continue;
            }

            if (fileList[i].getName() != null && fileList[i].getName().trim().startsWith(OSETFtpAdaptor.FILE_NAME_WIP))
            {
                try
                {
                    allWipLotList = parseOSETWip( fileList[i].getAbsolutePath() );
                    getWipStatusInfoList(allWipLotList);

                    //Add files to success list to move to /done directory
                    successFileList.add(fileList[i]);
                    success = true;
                 }
                catch( Exception e )
                {
                    success = false;
                    log("Exception while parsing file: " + fileList[i].getAbsolutePath() + "\nException: " + e);
                    e.printStackTrace();
                    throw e;
                }    
            }
        }


         /*Updating the Test Lot Information*/
        try
        {
           // Step 3
           // Save Test Information
           if( osetAssemblyList.isEmpty() == false )
           {

               log(" Updating Assembly  Lot Information in to Intermediate Table");
               OSETDAO.saveOSETAssemblyWipDetails( null, osetAssemblyList);
           }
           osetAssemblyList.clear();
           osetAssemblyList = null;
       }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Test Lot information into database: " + se);
            throw se;
        }

        /* Updating lots from Wip fed and OSET */
        try
        {
            m_assemblyDataMap = OSETDAO.getOSETAssemblyMap();
            log(" Updating OSET Assembly information from Wip Fed and OSET ");
            AssemblyAdaptorDAO.saveAssemblyLot(null, m_assemblyDataMap.values().iterator(), WIP_TYPE_ASSEMBLY, VENDOR_NAME_OSET);
            m_assemblyDataMap.clear();
            m_assemblyDataMap = null;
        }
        catch (Exception e)
        {
            success = false;
            e.printStackTrace();
        }

        /*Updating the Test Lot Information*/
        try
        {
           // Step 3
           // Save Test Information
           if( m_ftDataMap.isEmpty() == false )
           {
               log(" Updating Test Lot Information");
               TestAdaptorDAO.saveTestLot( null,  m_ftDataMap.values().iterator(), LOCATION_TYPE_FT, VENDOR_NAME_OSET );
           }
           else
           {
               TestAdaptorDAO.closeTestLot( null, LOCATION_TYPE_FT, VENDOR_NAME_OSET );
           }

           m_ftDataMap.clear();
           m_ftDataMap = null;
       }
       catch (ScalarException se)
       {
           success = false;
           se.printStackTrace();
           log("ERROR!! ERROR!! ERROR!! Could not update Test Lot information into database: " + se);
           throw se;
       }

        try
        {
            // Step 2
            // Save Class Store Information
            if (m_trDataMap.isEmpty() == false)
            {
                log(" Updating ISPL Tape and Reel Lot Information");
                InventoryLotDAO.saveInventoryLot(null, m_trDataMap.values().iterator(), LOCATION_TYPE_TAPE_REEL, VENDOR_NAME_OSET);
            }
            else
            {
                InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_TAPE_REEL, VENDOR_NAME_OSET);
            }
            m_trDataMap.clear();
            m_trDataMap = null;
            success = true;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Tape and Reel Lot information into database: " + se);
            throw se;
        }
        //Updating the OSEP FG Lot Information*/
        try
        {
            // Step 2
            // Save Finished Goods Information
            if( m_fgDataMap.isEmpty() == false )
            {
                log(" Updating OSEP Finished Goods Lot Information");
                InventoryLotDAO.saveInventoryLot( null, m_fgDataMap.values().iterator(), LOCATION_TYPE_FG, VENDOR_NAME_OSET);
            }
            else
            {
                InventoryLotDAO.closeInventoryLot( null, LOCATION_TYPE_FG, VENDOR_NAME_OSET );
            }
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Finished Goods Lot information into database: " + se);
            throw se;
        }

        // TO SEND THE NEW LOTS, NEW TRANSIT LOTS, NO PO LOTS, DISCREPANCY LOTS NOTIFICATIONS TO THE USER
        AdaptorDAO.sendAllNotifications(VENDOR_NAME_OSET, true, true, false);

        // To Close All Drop Out Lots
        AdaptorDAO.closeDropOutLots(VENDOR_NAME_OSET);

        int adaptor_id = AdaptorRunInfo.getLastRunAdaptorId( OSETFtpAdaptor.ADAPTOR_NAME, CONSTANT_ADAPTOR);

        // Update Adaptor_Run_Info table
        try
        {
            if ( success == true )
            {
                AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, OSETFtpAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
                System.out.println( "SUCCESSFULLY UPDATED IN THE DATABASE" );
            }
        }
        catch ( ScalarException se )
        {
            success = false; //As filES DATA SAVED , UPDATEING AS SUCCESS
            AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, OSETFtpAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
            log( "WARNING!! WARNING!! WARNING!! Could not update Adaptor run information, Exception: " + se );
            throw se;
        }
         // Step 7
       // Move the successful files to 'done' directory
       if ( success )
       {
           AdaptorUtils.moveFilesToDone( successFileList, doneDestDir );
       }
    }


    private ArrayList parseOSETWip( String fileName ) throws Exception
    {
        OSEPWipStatus osetWipStatus = null;
        ArrayList osetWipList = new ArrayList();
        Sheet sheet = null;
        String checkValue = null;

        if ( fileName == null )
        {
           throw new ScalarException( "Filename argument is null" );
        }

        try
        {
            File wipStatusFile = new File( fileName );
            if ( wipStatusFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "Parsing the file for OSET WIP Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "Parsing the file for OSET WIP Report..." );

            Workbook workbook = Workbook.getWorkbook( wipStatusFile );

            sheet = workbook.getSheet(0);

            Cell[] row = null;

            for ( int rowCount = 9; rowCount < sheet.getRows(); rowCount++ )
            {


                row = sheet.getRow(rowCount);
                if (row != null && row.length > 15)
                {
                    checkValue = row[0].getContents().trim();
                    if (checkValue != null && !"Grand Total:".equalsIgnoreCase( checkValue ) )
                    {
                        osetWipStatus = new OSEPWipStatus();

                        if (row.length > 0)
                        {
                            if (row[0].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setDevice(null);
                            }
                            else
                            {
                                osetWipStatus.setDevice(row[0].getContents().trim());
                            }
                        }
                        if (row.length > 1)
                        {
                            if (row[1].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setPkgType(null);
                            }
                            else
                            {
                                osetWipStatus.setPkgType(row[1].getContents().trim());
                            }
                        }
                        if (row.length > 2)
                        {
                            if (row[2].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setLotNo(null);
                            }
                            else
                            {
                                osetWipStatus.setLotNo(row[2].getContents().trim());
                            }
                        }
                        if (row.length > 3)
                        {
                            if (row[3].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setPoNo(null);
                            }
                            else
                            {
                                osetWipStatus.setPoNo(row[3].getContents().trim());
                            }
                        }
                        if (row.length > 4)
                        {
                            if (row[4].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setInvQty(null);
                            }
                            else
                            {
                                osetWipStatus.setInvQty(row[4].getContents().trim());
                            }
                        }
                        if (row.length > 5)
                        {
                            if (row[5].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setWfrQty(null);
                            }
                            else
                            {
                                osetWipStatus.setWfrQty(row[5].getContents().trim());
                            }
                        }
                        if (row.length > 6)
                        {
                            if (row[6].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setWaferTest(null);
                            }
                            else
                            {
                                osetWipStatus.setWaferTest(row[6].getContents().trim());
                            }
                        }
                        if (row.length > 7)
                        {
                            if (row[7].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setStock(null);
                            }
                            else
                            {
                                osetWipStatus.setStock(row[7].getContents().trim());
                            }
                        }
                         if (row.length > 8)
                        {
                            if (row[8].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setWaferInk(null);
                            }
                            else
                            {
                                osetWipStatus.setWaferInk(row[8].getContents().trim());
                            }
                        }
                        if (row.length > 9)
                        {
                            if (row[9].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setVisualInspection(null);
                            }
                            else
                            {
                                osetWipStatus.setVisualInspection(row[9].getContents().trim());
                            }
                        }
                        if (row.length > 10)
                        {
                            if (row[10].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setDieAttach(null);
                            }
                            else
                            {
                                osetWipStatus.setDieAttach(row[10].getContents().trim());
                            }
                        }
                        if (row.length > 11)
                        {
                            if (row[11].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setWireBond(null);
                            }
                            else
                            {
                                osetWipStatus.setWireBond(row[11].getContents().trim());
                            }
                        }
                        if (row.length > 12)
                        {
                            if (row[12].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setPreMoldInspection(null);
                            }
                            else
                            {
                                osetWipStatus.setPreMoldInspection(row[12].getContents().trim());
                            }
                        }
                        if (row.length > 13)
                        {
                            if (row[13].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setMolding(null);
                            }
                            else
                            {
                                osetWipStatus.setMolding(row[13].getContents().trim());
                            }
                        }
                        if (row.length > 14)
                        {
                            if (row[14].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setPostMoldCure(null);
                            }
                            else
                            {
                                osetWipStatus.setPostMoldCure(row[14].getContents().trim());
                            }
                        }
                        if (row.length > 15)
                        {
                            if (row[15].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setDejunkTrim(null);
                            }
                            else
                            {
                                osetWipStatus.setDejunkTrim(row[15].getContents().trim());
                            }
                        }
                        if (row.length > 16)
                        {
                            if (row[16].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setSolderPlating(null);
                            }
                            else
                            {
                                osetWipStatus.setSolderPlating(row[16].getContents().trim());
                            }
                        }
                        if (row.length > 17)
                        {
                            if (row[17].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setFormSingulation(null);
                            }
                            else
                            {
                                osetWipStatus.setFormSingulation(row[17].getContents().trim());
                            }
                        }
                        if (row.length > 18)
                        {
                            if (row[18].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setBufferStock(null);
                            }
                            else
                            {
                                osetWipStatus.setBufferStock(row[18].getContents().trim());
                            }
                        }
                        if (row.length >19)
                        {
                            if (row[19].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setFinalTest(null);
                            }
                            else
                            {
                                osetWipStatus.setFinalTest(row[19].getContents().trim());
                            }
                        }

                        if (row.length > 20)           //A2
                        {
                            if (row[20].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setFinalVisualInspection(null);
                            }
                            else
                            {
                                osetWipStatus.setFinalVisualInspection(row[20].getContents().trim());
                            }
                        }
                        if (row.length > 21)
                        {
                            if (row[21].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setTprQty(null);
                            }
                            else
                            {
                                osetWipStatus.setTprQty(row[21].getContents().trim());
                            }
                        }
                        if (row.length > 22)
                        {
                            if (row[22].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setFgBal(null);
                            }
                            else
                            {
                                osetWipStatus.setFgBal(row[22].getContents().trim());
                            }
                        }
                        if (row.length > 23)
                        {
                            if (row[23].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setRcvdDate(null);
                            }
                            else
                            {
                                osetWipStatus.setRcvdDate(row[23].getContents().trim());
                            }
                        }
                        if (row.length > 24)
                        {
                            if (row[24].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setInputDate(null);
                            }
                            else
                            {
                                osetWipStatus.setInputDate(row[24].getContents().trim());
                            }
                        }
                        if (row.length > 25)
                        {
                            if (row[25].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setAssyODate(null);
                            }
                            else
                            {
                                osetWipStatus.setAssyODate(row[25].getContents().trim());
                            }
                        }
                        if (row.length > 26)
                        {
                            if (row[26].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setRtnDate(null);
                            }
                            else
                            {
                                osetWipStatus.setRtnDate(row[26].getContents().trim());
                            }
                        }
                        if (row.length > 27)
                        {
                            if (row[27].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setrRtnDdate(null);
                            }
                            else
                            {
                                osetWipStatus.setrRtnDdate(row[27].getContents().trim());
                            }
                        }
                        if (row.length > 28)
                        {
                            if (row[28].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setProcessId(null);
                            }
                            else
                            {
                                osetWipStatus.setProcessId(row[28].getContents().trim());
                            }
                        }
                        if (row.length > 29)
                        {
                            if (row[29].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setCode(null);
                            }
                            else
                            {
                                osetWipStatus.setCode(row[29].getContents().trim());
                            }
                        }
                        if (row.length > 30)
                        {
                            if (row[30].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setCodeDescription(null);
                            }
                            else
                            {
                                osetWipStatus.setCodeDescription(row[30].getContents().trim());
                            }
                        }
                        if (row.length > 32)
                        {
                            if (row[32].getType().equals(CellType.EMPTY))
                            {
                                osetWipStatus.setDateCode(null);
                            }
                            else
                            {
                                osetWipStatus.setDateCode(row[32].getContents().trim());
                            }
                        }

                        osetWipList.add(osetWipStatus);
                    }
                }
            }
        }
        catch (Exception e)
        {
            throw new ScalarException("Exception " + e);
        }

        return osetWipList;
    }


    public void getWipStatusInfoList(ArrayList allWipLotList)
    {
        String partNumber       = null;
        String lotNumber        = null;
        OSEPWipStatus osetWipStatus = null;
        TestLot testLot = null;
        InventoryLot inventoryLot = null;
        int assQtyIn = 0;
        int testQtyIn = 0;
        int fgQqtyIn = 0;
        int invQtyIn = 0;

        try
        {
            Iterator osetIterator = allWipLotList.iterator();

            while (osetIterator.hasNext())
            {
                osetWipStatus = (OSEPWipStatus) osetIterator.next();

                partNumber = osetWipStatus.getDevice();
                lotNumber = osetWipStatus.getLotNo();
                assQtyIn = osetWipStatus.getAssemblyQtyIn();
                testQtyIn = osetWipStatus.getTestQtyIn();

                try {
                    fgQqtyIn = Integer.parseInt(osetWipStatus.getFgBal());
                }
                catch (NumberFormatException ignore) {
                    fgQqtyIn = 0;
                }

                try {
                    invQtyIn = Integer.parseInt(osetWipStatus.getInvQty());
                }
                catch (NumberFormatException ignore) {
                    invQtyIn = 0;
                }

                if (testQtyIn == 0 && fgQqtyIn == 0 && assQtyIn == 0)
                {
                    if (invQtyIn > 0)
                    {
                        assQtyIn = invQtyIn;
                    }
                }

                String dateIn    = null;
                String assyExpdateOut = null;
                if ( osetWipStatus.getInputDate() != null && osetWipStatus.getInputDate().trim().length() < 8 )
                {
                    String[] inputDate = osetWipStatus.getInputDate().split("/");
                    int Month = Integer.parseInt( inputDate[0] );
                    if ( Month > thisMonth )
                    {
                        dateIn = osetWipStatus.getInputDate() + "/" + ( thisYear-1);
                    }
                    else
                    {
                        dateIn = osetWipStatus.getInputDate() + "/" + thisYear;
                    }
                }
                if ( dateIn == null && osetWipStatus.getRcvdDate() != null && osetWipStatus.getRcvdDate().trim().length() < 8 )
                {
                    String[] recDate = osetWipStatus.getRcvdDate().split("/");
                    int Month = Integer.parseInt( recDate[0] );
                    if ( Month > thisMonth )
                    {
                        dateIn = osetWipStatus.getRcvdDate() + "/" + ( thisYear-1);
                    }
                    else
                    {
                        dateIn = osetWipStatus.getRcvdDate() + "/" + thisYear;
                    }
                }
                if (osetWipStatus.getAssyODate() != null && osetWipStatus.getAssyODate().trim().length() < 8 )
                {
                    String[] recDate = osetWipStatus.getAssyODate().split("/");
                    int Month = Integer.parseInt( recDate[0] );
                    if ( Month > thisMonth )
                    {
                        assyExpdateOut = osetWipStatus.getAssyODate() + "/" + ( thisYear-1);
                    }
                    else
                    {
                        assyExpdateOut = osetWipStatus.getAssyODate() + "/" + thisYear;
                    }
                }

                /*Assembly Lots*/
                if (partNumber != null && lotNumber != null && (assQtyIn > 0))
                {
                    int releaseCount = AdaptorDAO.getApprovedAssemblyReleaseLot( lotNumber, partNumber, VENDOR_NAME_OSET );

                    if ( releaseCount > 0 )
                    {
                        AdaptorDAO.updateAssemblyReleaseStatus( lotNumber );
                    }
                    osetAssemblyList.add(osetWipStatus);
                }
                /*Test Lots*/
                if (partNumber != null && lotNumber != null && testQtyIn > 0)
                {
                    testLot = (TestLot) m_ftDataMap.get(lotNumber + partNumber);
                    if (testLot == null)
                    {
                        testLot = new TestLot();
                        m_ftDataMap.put(lotNumber + partNumber, testLot);
                    }

                    int quantity = 0;
                    if (testLot.getQtyIn() != null)
                    {
                        try
                        {
                            quantity = Integer.parseInt(testLot.getQtyIn());
                        }
                        catch (NumberFormatException ignore) {
                        }
                        quantity += testQtyIn;
                    }
                    else
                    {
                        quantity = testQtyIn;
                    }
                    if ( quantity >0 )
                    {
                        testLot.setPartNumber(partNumber);
                        testLot.setPackageType(osetWipStatus.getPkgType());
                        testLot.setLotNumber(lotNumber);
                        testLot.setQtyIn(quantity + "");
                        testLot.setDateCode(osetWipStatus.getDateCode());
                        testLot.setPoNumber(osetWipStatus.getPoNo());
                        testLot.setStatus(LOT_STATUS_RUNNING);
                        testLot.setVendorName(VENDOR_NAME_OSET);
                        testLot.setPsType(LOCATION_TYPE_FT);
                        testLot.setCategory(LOT_CATEGORY_TYPE_PROD);
                        testLot.setFabLotNumber(lotNumber);
                        testLot.setDateIn(dateIn);
                        testLot.setExpecetedDate( assyExpdateOut );
                    }

                }

                 /*Tape and Reel Lots*/
                if (partNumber != null && lotNumber != null && osetWipStatus.getTprQty() != null)
                {
                    inventoryLot = (InventoryLot) m_trDataMap.get(lotNumber + partNumber);
                    if (inventoryLot == null)
                    {
                        inventoryLot = new InventoryLot(VENDOR_NAME_OSET, LOCATION_TYPE_TAPE_REEL);
                        m_trDataMap.put(lotNumber + partNumber, inventoryLot);
                    }

                    int quantity = 0;
                    if (inventoryLot.getQtyIn() != null)
                    {
                        try
                        {
                            quantity = Integer.parseInt(inventoryLot.getQtyIn());
                        }
                        catch (NumberFormatException ignore) {
                        }
                        try
                        {
                        quantity += Integer.parseInt(osetWipStatus.getTprQty() );
                        }
                        catch (NumberFormatException ignore) {
                        }
                    }
                    else
                    {
                        try
                        {
                        quantity = Integer.parseInt(osetWipStatus.getTprQty() );
                        }
                        catch (NumberFormatException ignore) {
                        }
                    }

                    if ( quantity > 0 )
                    {
                        inventoryLot.setPartNumber(partNumber);
                        inventoryLot.setLotNumber(lotNumber);
                        inventoryLot.setFabLotNumber(lotNumber);
                        inventoryLot.setQtyIn(quantity + "");
                        inventoryLot.setDateCode(osetWipStatus.getDateCode());
                        inventoryLot.setStatus(LOT_STATUS_RUNNING);
                        inventoryLot.setLotCategory(LOT_CATEGORY_TYPE_PROD);
                        inventoryLot.setDateIn(dateIn);
                    }
                }
                /*FG Lots*/
                if (partNumber != null && lotNumber != null && fgQqtyIn > 0)
                {
                    inventoryLot = (InventoryLot) m_fgDataMap.get(lotNumber + partNumber);
                    if (inventoryLot == null)
                    {
                        inventoryLot = new InventoryLot( VENDOR_NAME_OSET, LOCATION_TYPE_FG);
                        m_fgDataMap.put(lotNumber + partNumber, inventoryLot);
                    }

                    int quantity = 0;
                    if (inventoryLot.getQtyIn() != null)
                    {
                        try
                        {
                            quantity = Integer.parseInt(inventoryLot.getQtyIn());
                        }
                        catch (NumberFormatException ignore) {
                        }
                        quantity += fgQqtyIn;
                    }
                    else
                    {
                        quantity = fgQqtyIn;
                    }

                    inventoryLot.setPartNumber(partNumber);
                    inventoryLot.setLotNumber(lotNumber);
                    inventoryLot.setFabLotNumber(lotNumber);
                    inventoryLot.setQtyIn(quantity + "");
                    inventoryLot.setDateCode(osetWipStatus.getDateCode());
                    inventoryLot.setStatus(LOT_STATUS_RUNNING);
                    inventoryLot.setLotCategory(LOT_CATEGORY_TYPE_PROD);
                    inventoryLot.setDateIn(dateIn);

                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            log("ERROR!ERROR! While parsing the OSET Details" + e);
            System.out.println("ERROR!ERROR! While parsing the OSET Details" + e);
        }
    }


    private void log(String message)
    {
        if (message == null) return;

        if (m_parserLogger != null)
        {
            synchronized (m_parserLogger)
            {
                m_parserLogger.logDebug(message + LINE_SEPARATOR);
            }
        }
        else
        {
            System.out.println(message);
        }
    }

    private void printVersionInfo()
    {
        String info = "\n\n+----------- Running OSET Wip Data Parser on " + new java.util.Date(System.currentTimeMillis()) + " -------------+\n" +
               "\n+---------------------------------------------------------------+\n" +
               "+                                                                \n" +
               "+                     OSET Wip Data Interpreter - Beta          \n" +
               "+                                                                \n" +
               "+    Copy Right (C) 2007 - ScalarSoft Inc., All rights reserved.  \n" +
               "+                                                                \n" +
               "+---------------------------------------------------------------+\n";
        System.out.println(info);
        log(info);
    } //end of printVersionInfo
}
